﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextMIS
{
    public partial class frmGrpSelect : Form
    {
        private dbopr baseinfo = new dbopr();
        public DataTable dt_PackItemRel = null;
        private DataTable dt_tj_zhxm_hd = null;
        private DataTable dt_tj_zhxm_hd_temp = null;
        private string str_Sex;

        public frmGrpSelect(DataTable dt, string sex)
        {
            InitializeComponent();
            dt_PackItemRel = dt;
            str_Sex = sex;
        }

        private void frmGrpSelect_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            Tr_Grp.ImageList = imageList1;

            lv_Package.SmallImageList = imageList1;
            lv_Package.StateImageList = imageList1;
            lv_Package.LargeImageList = imageList1;

            lv_uncheckItem.SmallImageList = imageList1;
            lv_uncheckItem.StateImageList = imageList1;
            lv_uncheckItem.LargeImageList = imageList1;

            //套餐中已经包含的组别
            lv_CheckItem.Items.Clear();
            lv_CheckItem.View = View.SmallIcon;
            foreach (DataRow dr in dt_PackItemRel.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["Grp_id"].ToString().Trim();
                item.Text = dr["Gro_name"].ToString().Trim();
                lv_CheckItem.Items.Add(item);
            }

            //套餐包信息
            lv_Package.Items.Clear();
            lv_Package.View = View.Tile;
            DataTable dt_PackInfo = baseinfo.Get_PackList();
            foreach (DataRow dr in dt_PackInfo.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["Pack_id"].ToString().Trim();
                item.Text = dr["Pack_name"].ToString().Trim();
                item.ImageIndex = 5;
                lv_Package.Items.Add(item);
            }

            //体检科室中的组别-点击后在右边显示
            Tr_Grp.Nodes.Clear();
            TreeNode node = new TreeNode("体检科室");
            node.Tag = "00";
            DataTable dt = baseinfo.Get_Bran_list();
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode node1 = new TreeNode(dr["Bra_name"].ToString());
                node1.Tag = dr["Bra_id"].ToString();
                node.Nodes.Add(node1);
            }
            Tr_Grp.Nodes.Add(node);
            Tr_Grp.ExpandAll();
            //new Common.Common().AddImage(node);

            dt_tj_zhxm_hd = baseinfo.Get_GrpItm_Info();
        }

        //添加组合项目
        private void bt_ok_Click(object sender, EventArgs e)
        {
            dt_PackItemRel.Rows.Clear();
            foreach (ListViewItem item in lv_CheckItem.Items)
            {
                DataRow dr = dt_PackItemRel.NewRow();
                dr["Grp_id"] = item.Tag.ToString().Trim();
                dr["Gro_name"] = item.Text.ToString().Trim();
                dt_PackItemRel.Rows.Add(dr);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //通过选择科室将科室 相关组合项目体现在右侧
        private void Tr_Grp_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string str_BraId = Tr_Grp.SelectedNode.Tag.ToString().Trim();
            string str_sql = "(";
            foreach (ListViewItem item in lv_CheckItem.Items)
            {
                str_sql = str_sql + "'" + item.Tag.ToString().Trim() + "',";
            }
            str_sql = str_sql.Substring(0, str_sql.Length - 1);
            if (str_sql.Length > 0) str_sql = str_sql + ")";
            DataRow[] drs;
            if (str_BraId == "00")//所有结果检索
            {
                if (str_sql != "")
                    drs = dt_tj_zhxm_hd.Select("Gro_id not in" + str_sql);
                else
                    drs = dt_tj_zhxm_hd.Select("1=1");
            }
            else
            {
                if (str_sql != "")
                    drs = dt_tj_zhxm_hd.Select("Bra_id='" + str_BraId + "' and Gro_id not in" + str_sql);
                else
                    drs = dt_tj_zhxm_hd.Select("Bra_id='" + str_BraId + "'");
            }
            dt_tj_zhxm_hd_temp = dt_tj_zhxm_hd.Copy();
            dt_tj_zhxm_hd_temp.Rows.Clear();

            lv_uncheckItem.Items.Clear();
            lv_uncheckItem.View = View.Tile;
            foreach (DataRow dr in drs)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["Gro_id"].ToString().Trim();
                item.Text = dr["Gro_name"].ToString().Trim();
                item.ImageIndex = 6;
                lv_uncheckItem.Items.Add(item);

                DataRow dr_temp = dt_tj_zhxm_hd_temp.NewRow();
                dr_temp["Gro_id"] = dr["Gro_id"];
                dr_temp["Gro_name"] = dr["Gro_name"];
                dr_temp["Bra_id"] = dr["Bra_id"];
                dr_temp["Pinyin_code"] = dr["Pinyin_code"];
                dr_temp["Wubi_code"] = dr["Wubi_code"];
                //dr_temp["zdym"] = dr["zdym"];

                dt_tj_zhxm_hd_temp.Rows.Add(dr_temp);
            }

            if (Tr_Grp.SelectedNode.Nodes.Count == 0)
            {
                Tr_Grp.SelectedNode.SelectedImageIndex = Tr_Grp.SelectedNode.ImageIndex;
            }
        }
        //检索码变化--需要修改
        private void txt_jsm_TextChanged(object sender, EventArgs e)
        {
            if (object.Equals(null, dt_tj_zhxm_hd_temp)) return;
            string str_jsm = txt_jsm.Text.Trim();
            DataRow[] drs = dt_tj_zhxm_hd_temp.Select("pyjm like '%" + str_jsm + "%' or wbjm like '%" + str_jsm + "%' or zdym like '%" + str_jsm + "%'");
            lv_uncheckItem.Items.Clear();
            lv_uncheckItem.View = View.Tile;
            foreach (DataRow dr in drs)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["bh"].ToString().Trim();
                item.Text = dr["mc"].ToString().Trim();
                item.ImageIndex = 6;
                lv_uncheckItem.Items.Add(item);
            }
        }
        //套餐选择变化后左侧已选项目也要变化
        private void lv_Package_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lv_Package.GetItemAt(e.X, e.Y);
            if (object.Equals(null, item)) return;

            lv_CheckItem.Items.Clear();
            DataTable dt = baseinfo.Get_PackItemRel(item.Tag.ToString().Trim());
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem item1 = new ListViewItem();
                item1.Tag = dr["Grp_id"].ToString().Trim();
                item1.Text = dr["Gro_name"].ToString().Trim();
                lv_CheckItem.Items.Add(item1);
            }
        }

        //未选择组合项目双击后选择上
        private void lv_uncheckItem_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lv_uncheckItem.GetItemAt(e.X, e.Y);
            if (object.Equals(null, item)) return;

            registopr tjglbiz = new registopr();
            if (tjglbiz.CheckSex(item.Tag.ToString().Trim(), str_Sex) <= 0)
            {
                MessageBox.Show("所选择的项目【编号：" + item.Tag.ToString().Trim() + "】存在与性别不匹配，或者组合项目明细为空，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            ListViewItem item1 = new ListViewItem();
            item1.Tag = item.Tag;
            item1.Text = item.Text;
            lv_CheckItem.Items.Add(item1);
            lv_uncheckItem.Items.Remove(item);
        }
        //已选项目双击后到非选择区
        private void lv_CheckItem_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lv_CheckItem.GetItemAt(e.X, e.Y);
            if (object.Equals(null, item)) return;

            try
            {
                lv_CheckItem.Items.Remove(lv_CheckItem.SelectedItems[0]);

            }
            catch
            {

            }
        }



    }
}
